package com.wmh.baseservice.common.utils;

import com.wmh.baseservice.common.utils.encrypt.MD5Util;

import java.util.concurrent.atomic.AtomicLong;

public class UUIDUtil {

    private static AtomicLong timeMills = new AtomicLong(0);

    /**
     * 获取唯一时间戳值
     */
    public static Long getTimeId() {

        while (true) {
            long currentTimeMillis = System.nanoTime();
            long currentMill = timeMills.get();
            if (currentTimeMillis > currentMill && timeMills.compareAndSet(currentMill, currentTimeMillis)) {
                //返回唯一时间戳
                return currentTimeMillis;
            }
        }
    }

    /**
     * 获取 唯一码
     */
    public static String getCode() {
        Long timeId = getTimeId();
        return MD5Util.MD5Encode(timeId.toString(), "UTF-8");
    }

    /**
     * 获取随机一位26字母
     */
    public static String getLetter() {
        //需要生成几位
        int n = 1;
        //最终生成的字符串
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < n; i++) {
            str.append((char) (Math.random() * 26 + 'A'));
        }
        return str.toString();
    }
}
